package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFinancialDataMapper;
import com.csun.dsms.mapper.HgFinancialProjectMapper;
import com.csun.dsms.model.huang.HgFinancialData;
import com.csun.dsms.model.huang.HgFinancialProjectData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.service.HgFinancialDataService;
import com.csun.dsms.service.HgFinancialProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgFinancialProjectServiceImpl extends BaseService<HgFinancialProjectData>
        implements HgFinancialProjectService {

    @Resource
    private HgFinancialProjectMapper hgFinancialProjectMapper;
    
    @Override
    public List<HgFinancialProjectData> queryProjectList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgFinancialProjectMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Wrapper importFinancialProject(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFinancialProjectData hgFinancialProjectData = new HgFinancialProjectData();
                hgFinancialProjectData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgFinancialProjectData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgFinancialProjectData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgFinancialProjectData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgFinancialProjectData.setIndustryName(Convert.toStr(excelMap.get("行业名称")));
                hgFinancialProjectData.setProjectName(Convert.toStr(excelMap.get("项目名称")));
                hgFinancialProjectData.setProjectFund(Convert.toDouble(excelMap.get("项目基金")));
                hgFinancialProjectData.setApplicationBatchName(Convert.toStr(excelMap.get("应用程序批处理名称")));
                hgFinancialProjectData.setSuggestedFollow(Convert.toStr(excelMap.get("建议关注")));
                hgFinancialProjectData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgFinancialProjectData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgFinancialProjectData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgFinancialProjectData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgFinancialProjectData.setCreatedTime(new Date());
                hgFinancialProjectMapper.insert(hgFinancialProjectData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入争资争项项目清单数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
